//
//  ReportViewController.m
//  zhixinApp
//
//  Created by zhiguanzhl on 2017/12/5.
//  Copyright © 2017年 zhiguanzhl. All rights reserved.
//

#import "ReportViewController.h"
#import "SBButton.h"
#import "DistributeViewController.h"

@interface ReportViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *segmentView;
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic, strong)UIView *bgView;
@property (nonatomic, weak) UIButton *button;
@property (nonatomic, weak) UILabel *lable;
@property (nonatomic, assign)NSInteger selectRow;
@property (nonatomic,strong)NSNumber *iid;

@end

@implementation ReportViewController


- (void)viewDidLoad {
    
    [super viewDidLoad];
    
}


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBarHidden = NO;
    
    self.navigationItem.titleView = [self titleView];

}

- (UIView *)titleView{
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(15, 0, kSCREEN_WIDTH-30, 30)];
    UILabel *lable = [UILabel labelWithFrame:CGRectZero Font:FONT(16) TextColor:[UIColor colorWithHexString:@"454545"]];
    NSArray *companyModelArr = [DataCenter shareInstence].companyList;
    CompanyModel *model = companyModelArr[0];
    lable.text = model.name;
    [lable sizeToFit];
    lable.centerY = view.centerY;
    self.lable = lable;
    [view addSubview:lable];
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(lable.right+10, 2.5, 25, 25)];
    [button addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [button setImage:[UIImage imageNamed:@"open_gongsixuanze_i"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"open_gongsixuanze_top_i"] forState:UIControlStateSelected];
    [button setImage:[UIImage imageNamed:@"open_gongsi_i"] forState:UIControlStateDisabled];
    [view addSubview:button];
    self.button = button;
    
    self.button.enabled = (companyModelArr.count>1)?YES:NO;

    
    self.iid = model.companyId;

    return view;
}

- (void)btnAction:(UIButton *)btn{
    btn.selected = !btn.selected;
    btn.selected?[self show]:[self hideTab];
}

- (IBAction)reportAction:(SBButton *)sender {
    
    if (sender.tag == 11) {
        DistributeViewController *dictributeVc = [[DistributeViewController alloc] init];
        dictributeVc.companyID = self.iid;
        [self.navigationController pushViewController:dictributeVc animated:YES];
        
    }else{
        iToast *itost = [[iToast alloc] initWithText:@"小智正在加速为您开发，敬请期待"];
        [itost show];
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return  44;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [DataCenter shareInstence].companyList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *cellID = @"TableViewCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSArray *companyModelArr = [DataCenter shareInstence].companyList;
    CompanyModel *model = companyModelArr[indexPath.row];
    cell.textLabel.text = model.name;
   
    cell.textLabel.textColor =
    (self.selectRow==indexPath.row)?
    [UIColor colorWithHexString:@"2C9AE8"]:
    [UIColor colorWithHexString:@"999999"]
    ;
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    self.selectRow = indexPath.row;

    NSArray *companyModelArr = [DataCenter shareInstence].companyList;
    CompanyModel *model = companyModelArr[indexPath.row];
    self.iid = model.companyId;
    self.lable.text = model.name;
    [self.lable sizeToFit];

    self.button.left = self.lable.right+10;
    [tableView reloadData];
    
    [self hideTab];
}


- (void)show{
    [UIView animateWithDuration:0.25 animations:^{
        self.tableView.height = [DataCenter shareInstence].companyList.count*44.0;
        self.bgView.hidden = NO;
    }];
    
}
- (void)hideTab{
    self.button.selected = NO;
    [UIView animateWithDuration:0.25 animations:^{
        self.tableView.height = 0;
    }completion:^(BOOL finished) {
        self.bgView.hidden = YES;
    }];
    
}
- (UIView *)bgView{
    if (!_bgView) {
        _bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, kSCREEN_HEIGHT-64)];
        UIView *blockView = [[UIView alloc]initWithFrame:_bgView.frame];
        blockView.backgroundColor = [UIColor blackColor];
        blockView.alpha = 0.3;
        
        UITapGestureRecognizer *tapRenderer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideTab)];
        tapRenderer.numberOfTouchesRequired = 1;
        tapRenderer.numberOfTapsRequired = 1;
        [blockView addGestureRecognizer:tapRenderer];
        [_bgView addSubview:blockView];
        [_bgView addSubview:self.tableView];
        
        [self.view addSubview:_bgView];
    }
    return _bgView;
}
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, 0) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [[UIView alloc]init];
    }
    return _tableView;
}



@end
